package com.jfinal.weixin.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.ApiConfig;

/**
 * 
 * @author Javen
 * @Email javenlife@126.com
 * 公众平台通用接口工具类
 */
public class WeixinUtil {
	
	// 微信JSSDK的AccessToken请求URL地址
	public final static String weixin_jssdk_acceToken_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
	// 微信JSSDK的ticket请求URL地址 
	public final static String weixin_jssdk_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi"; 

	
	/**
	 * 获取配置
	 */
	public static ApiConfig getApiConfig() {
		ApiConfig ac = new ApiConfig();

		// 配置微信 API 相关常量
		ac.setToken(PropKit.get("token"));
		ac.setAppId(PropKit.get("appId"));
		ac.setAppSecret(PropKit.get("appSecret"));

		/**
		 *  是否对消息进行加密，对应于微信平台的消息加解密方式：
		 *  1：true进行加密且必须配置 encodingAesKey
		 *  2：false采用明文模式，同时也支持混合模式
		 */
		ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
		ac.setEncodingAesKey(PropKit.get("encodingAesKey", "setting it in config file"));
		return ac;
	}

	/**
	 * emoji表情转换(hex -> utf-16)
	 *
	 * @param hexEmoji
	 * @return
	 */
	public static String emoji(int hexEmoji) {
		return String.valueOf(Character.toChars(hexEmoji));
	}

	/**
	 * UTF-8编码
	 *
	 * @param source
	 * @return
	 */
	public static String urlEncodeUTF8(String source) {
		try {
			return URLEncoder.encode(source, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}

	
	/**
     * 获取微信JSSDK的access_token 
     * @author Benson
     */
    public static String getJSSDKAccessToken() {  
    	ApiConfig ac = getApiConfig();
		String appId = ac.getAppId();
		String appSecret = ac.getAppSecret();
        String returnString="";
        String requestUrl = String.format(weixin_jssdk_acceToken_url, appId, appSecret);  
        String result = HttpRequestUtils.get(requestUrl);  //Http GET请求
        JSONObject jsonObject = JSONObject.parseObject(result);
        // 如果请求成功   
        if (null != jsonObject) {  
            try {  
                returnString=jsonObject.getString("access_token");  
            } catch (JSONException e) {  
                returnString = null;  
            }  
        }  
        return returnString;  
    } 
     
    /**
     * 获取微信JSSDK的ticket 
     * @author Benson
     */
    public static String getJSSDKTicket(String access_token) {  
        String returnString="";
        String requestUrl = String.format(weixin_jssdk_ticket_url, access_token);  
        String result = HttpRequestUtils.get(requestUrl);  
        JSONObject jsonObject = JSONObject.parseObject(result);
        // 如果请求成功   
        if (null != jsonObject) {  
            try {  
                returnString=jsonObject.getString("ticket");  
            } catch (JSONException e) {  
                returnString = null;  
            }  
        }  
        return returnString;  
    } 
	
}